package com.zone.gateway.center.infrastructure.repository;

import com.zone.gateway.center.domain.register.model.entity.ApplicationInterfaceEntity;
import com.zone.gateway.center.domain.register.model.entity.ApplicationInterfaceMethodEntity;
import com.zone.gateway.center.domain.register.model.entity.ApplicationSystemEntity;
import com.zone.gateway.center.domain.register.repository.IRegisterManageRepository;
import com.zone.gateway.center.infrastructure.dao.IApplicationInterfaceDao;
import com.zone.gateway.center.infrastructure.dao.IApplicationInterfaceMethodDao;
import com.zone.gateway.center.infrastructure.dao.IApplicationSystemDao;
import com.zone.gateway.center.infrastructure.po.ApplicationInterface;
import com.zone.gateway.center.infrastructure.po.ApplicationInterfaceMethod;
import com.zone.gateway.center.infrastructure.po.ApplicationSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2025/4/24
 */
@Repository
public class RegisterManageRepository implements IRegisterManageRepository {
    @Autowired
    private IApplicationSystemDao applicationSystemDao;
    @Autowired
    private IApplicationInterfaceDao applicationInterfaceDao;
    @Autowired
    private IApplicationInterfaceMethodDao applicationInterfaceMethodDao;


    @Override
    public void regisApplicationSystem(ApplicationSystemEntity applicationSystemEntity) {
        ApplicationSystem existedSystem = applicationSystemDao.queryApplicationSystem(applicationSystemEntity.getSystemId());
        ApplicationSystem applicationSystem = new ApplicationSystem();
        applicationSystem.setSystemId(applicationSystemEntity.getSystemId());
        applicationSystem.setSystemName(applicationSystemEntity.getSystemName());
        applicationSystem.setSystemType(applicationSystemEntity.getSystemType());
        applicationSystem.setSystemRegistry(applicationSystemEntity.getSystemRegistry());
        if (existedSystem != null) {
            applicationSystemDao.update(applicationSystem);
        } else {
            applicationSystemDao.insert(applicationSystem);
        }
    }

    @Override
    public void regisApplicationInterface(ApplicationInterfaceEntity applicationInterfaceEntity) {
        ApplicationInterface applicationInterface = new ApplicationInterface();
        applicationInterface.setSystemId(applicationInterfaceEntity.getSystemId());
        applicationInterface.setInterfaceId(applicationInterfaceEntity.getInterfaceId());
        applicationInterface.setInterfaceName(applicationInterfaceEntity.getInterfaceName());
        applicationInterface.setInterfaceVersion(applicationInterfaceEntity.getInterfaceVersion());

        ApplicationInterface existedInterface = applicationInterfaceDao.queryApplicationInterface(applicationInterfaceEntity.getSystemId(), applicationInterfaceEntity.getInterfaceId());
        if (null != existedInterface) {
            applicationInterfaceDao.update(applicationInterface);
        } else {
            applicationInterfaceDao.insert(applicationInterface);
        }
    }

    @Override
    public void regisApplicationInterfaceMethod(ApplicationInterfaceMethodEntity applicationInterfaceMethodEntity) {
        ApplicationInterfaceMethod applicationInterfaceMethod = new ApplicationInterfaceMethod();
        applicationInterfaceMethod.setSystemId(applicationInterfaceMethodEntity.getSystemId());
        applicationInterfaceMethod.setInterfaceId(applicationInterfaceMethodEntity.getInterfaceId());
        applicationInterfaceMethod.setMethodId(applicationInterfaceMethodEntity.getMethodId());
        applicationInterfaceMethod.setMethodName(applicationInterfaceMethodEntity.getMethodName());
        applicationInterfaceMethod.setParameterType(applicationInterfaceMethodEntity.getParameterType());
        applicationInterfaceMethod.setUri(applicationInterfaceMethodEntity.getUri());
        applicationInterfaceMethod.setHttpCommandType(applicationInterfaceMethodEntity.getHttpCommandType());
        applicationInterfaceMethod.setAuth(applicationInterfaceMethodEntity.getAuth());

        ApplicationInterfaceMethod existedMethod = applicationInterfaceMethodDao.queryApplicationInterfaceMethod(applicationInterfaceMethodEntity.getSystemId(), applicationInterfaceMethodEntity.getInterfaceId(), applicationInterfaceMethodEntity.getMethodId());
        if (null != existedMethod) {
            applicationInterfaceMethodDao.update(applicationInterfaceMethod);
        } else {
            applicationInterfaceMethodDao.insert(applicationInterfaceMethod);
        }
    }
}
